using System;
using Azure.Domain.Messages;
using Azure.Domain.Models;
using Rhino.Mocks;
using Xunit;

namespace Azure.Tests.Domain.ServiceFacts
{
    public class UpdatePostFacts : ServiceFactBase
    {
        [Fact]
        public void EnsureThatAPostCannoBeUpdatedIfItDoesNotExist()
        {
            var post = Test.Post;
            var message = new UpdatePostMessage
            {
                Body = "updated",
                Title = "new title",
                Slug = "edit-test",
                PartitionKey = post.PartitionKey,
                RowKey = post.RowKey
            };
            Assert.Throws<InvalidOperationException>(() => ClassUnderTest.UpdatePost(message));
        }

        [Fact]
        public void UpdatePostWillLoadItIntoCache()
        {
            var post = Test.Post;
            Cache.Stub(x => x.Get(post.PartitionKey, post.RowKey)).Return(post);
            var message = new UpdatePostMessage
            {
                Body = "updated",
                Title = "new title",
                Slug = "edit-test",
                PartitionKey = post.PartitionKey,
                RowKey = post.RowKey
            };
            ClassUnderTest.UpdatePost(message);

            Cache.AssertWasCalled(x => x.Add(Arg<Post>.Is.Anything));
        }

        [Fact]
        public void UpdatePostWillUpdateTheRepository()
        {
            var post = Test.Post;
            Repository.Stub(x => x.Load(post.PartitionKey, post.RowKey)).Return(post) ;
            var message = new UpdatePostMessage
                              {
                                  Body = "updated",
                                  Title = "new title",
                                  Slug = "edit-test",
                                  PartitionKey = post.PartitionKey,
                                  RowKey = post.RowKey
                              };
            ClassUnderTest.UpdatePost(message);

            Repository.AssertWasCalled(x => x.Update(Arg<Post>.Is.Anything));
        }
    }
}